<?xml version="1.0"?>
<xsl:stylesheet xmlns:x="http://www.xopus.com/xmlns/config" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0" exclude-prefixes="xsl">

  <xsl:output encoding="us-ascii" method="html" version="1.0"
        doctype-public="-//W3C//DTD HTML 4.01 Transitional//EN"
        doctype-system="http://www.w3.org/TR/html4/loose.dtd" />

  <xsl:param name="q" select="''"/>
  <xsl:param name="t" select="''"/>

  <xsl:template match="files">
    <html>
      <head>
        <title>Reference Lookup</title>
        <link rel="stylesheet" type="text/css" href="ui/example-app.css"/>
      </head>
      <body class="lookup-ui-dialog">
        <form class="lookup-ui-form" method="get" action="lookup-reference.php">
          <div class="lookup-ui-panel">
            <div>
              <strong>Search for a reference</strong>
            </div>
            <input type="text" class="lookup-ui-textinput" name="q" value="{$q}"/>
            <xsl:text> </xsl:text>
            <select name="t" class="lookup-ui-dropdown">
              <option value="">[type]</option>
              <xsl:apply-templates select="file" mode="type">
                <xsl:sort select="@type" data-type="text" order="descending"/>
              </xsl:apply-templates>
            </select>
            <xsl:text> </xsl:text>
            <input type="submit" value="Search"/>
          </div>
        </form>
        <div class="lookup-ui-panel">
          <strong>
            <xsl:text>Choose a reference from the </xsl:text>
            <xsl:choose>
              <xsl:when test="$q = '' and $t = ''">
                <xsl:text>collection</xsl:text>
              </xsl:when>
              <xsl:otherwise>
                <a href="lookup-reference.php">collection</a>
              </xsl:otherwise>
            </xsl:choose>
          </strong>
          <table cellspacing="0">
            <thead>
              <th>Title</th>
              <th>Type</th>
            </thead>
            <tbody>
            <xsl:choose>
              <xsl:when test="$q = ''">
                <xsl:choose>
                  <xsl:when test="$t = ''">
                    <xsl:apply-templates select="file">
                      <xsl:sort select="@type" data-type="text" order="descending"/>
                    </xsl:apply-templates>
                  </xsl:when>
                  <xsl:otherwise>
                    <xsl:apply-templates select="file[@type = $t]">
                      <xsl:sort select="@type" data-type="text" order="descending"/>
                    </xsl:apply-templates>
                  </xsl:otherwise>
                </xsl:choose>
              </xsl:when>
              <xsl:otherwise>
                <xsl:choose>
                  <xsl:when test="$t = ''">
                    <xsl:apply-templates select="file[contains(.,$q) or contains(@src,$q)]">
                      <xsl:sort select="@type" data-type="text" order="descending"/>
                    </xsl:apply-templates>
                  </xsl:when>
                  <xsl:otherwise>
                    <xsl:apply-templates select="file[contains(.,$q) or contains(@src,$q) and @type = $t]">
                      <xsl:sort select="@type" data-type="text" order="descending"/>
                    </xsl:apply-templates>
                  </xsl:otherwise>
                </xsl:choose>
              </xsl:otherwise>
            </xsl:choose>
            </tbody>
          </table>
        </div>
        <script type="text/javascript" src="ui/example-app.js"></script>
      </body>
    </html>
  </xsl:template>

  <xsl:template match="file">
    <tr class="file">
      <td>
        <a href="{@href}" onclick="return lookup.chooseReference('{@href}');">
          <xsl:value-of select="."/>
        </a>
      </td>
      <td>
        <xsl:value-of select="@type"/>
      </td>
    </tr>
  </xsl:template>
  
  <xsl:template match="file" mode="type">
    <xsl:if test="not(preceding-sibling::file[@type = current()/@type])">
      <option value="{@type}">
        <xsl:value-of select="@type"/>
      </option>
    </xsl:if>
  </xsl:template>

</xsl:stylesheet>
